package cn.xaut.动态规划;

import static java.lang.Math.*;
import java.util.Arrays;

/**
 * 322. 零钱兑换
 */
public class demo322_2 {

    public int coinChange(int[] coins, int amount) {

        // dp[i] 表示凑成 i 的金额所需的最少硬币的个数
        int[] dp = new int[amount + 1];
        Arrays.fill(dp, amount + 1);
        dp[0] = 0;
        for (int i = 1; i <= amount; i++) {
            for (int coin : coins) {
                if (i >= coin) 
                    dp[i] = min(dp[i], dp[i - coin] + 1);
            }
        }
//        System.out.println(Arrays.toString(dp));
        
        return dp[amount] == amount + 1 ? -1 : dp[amount];
    }

    public static void main(String[] args) {

        System.out.println(new demo322_2().coinChange(new int[]{1, 2, 5}, 11));// 3
        System.out.println(new demo322_2().coinChange(new int[]{2}, 3));// -1
        System.out.println(new demo322_2().coinChange(new int[]{1}, 0));// 0
        System.out.println(new demo322_2().coinChange(new int[]{1}, 1));// 1
        System.out.println(new demo322_2().coinChange(new int[]{1}, 2));// 2    
    }
}
